import os
import sys
import time
import hashlib

score = 0

def clear_file(l):
    for i in l:
        with open(f'sound_card_sim/{i}.wav', 'wb') as f:
            f.write(b'')
def get_md5(l):
    r = []
    for i in l:
        with open(f'sound_card_sim/{i}.wav', 'rb') as f:
            r.append(hashlib.md5(f.read()).hexdigest())
    return r

print('开始评分')

print('..测试单进程')
clear_file(['dsp', 'out1'])
os.system('~/riscv-isa-sim/spike --extlib=sound_card_sim/sim.so --device=sound_card_sim,0x10000000,0 obj/thinos obj/apps/app_sound0')
yequ10s, dsp, out1 = get_md5(['yequ10s', 'dsp', 'out1'])
if yequ10s != dsp:
    print('....输出到声卡文件的数据错误')
    print(f'评分结束,得分:{score}/100')
    sys.exit(0)
print('....输出到声卡文件的数据正确')
score += 25
if yequ10s != out1:
    print('....从声卡文件读取的数据错误')
    print(f'评分结束,得分:{score}/100')
    sys.exit(0)
print('....从声卡文件读取的数据正确')
score += 35
print('..单进程正确')

print('..测试多进程')
clear_file(['dsp', 'out1', 'out2'])
os.system('~/riscv-isa-sim/spike --extlib=sound_card_sim/sim.so --device=sound_card_sim,0x10000000,0 obj/thinos obj/apps/app_sound1')
if os.stat('sound_card_sim/yequ10s.wav').st_size * 2 != os.stat('sound_card_sim/dsp.wav').st_size:
    print('....输出到声卡文件的数据错误')
    print(f'评分结束,得分:{score}/100')
    sys.exit(0)
print('....输出到声卡文件的数据正确')
score += 5
out1, out2 = get_md5(['out1', 'out2'])
if yequ10s != out1 or yequ10s != out2:
    print('....从声卡文件读取的数据错误')
    print(f'评分结束,得分:{score}/100')
    sys.exit(0)
print('....从声卡文件读取的数据正确')
score += 15
print('..多进程正确')

print('..测试中断和挂起')
clear_file(['dsp', 'out1', 'out2'])
t = time.time()
os.system('~/riscv-isa-sim/spike --extlib=sound_card_sim/sim.so --device=sound_card_sim,0x10000000,0 obj/thinos obj/apps/app_sound2')
t = time.time() - t
print(f'....测试程序3运行{t}秒')
if t > 9:
    print(f'....超过规定时间9秒')
    print(f'评分结束,得分:{score}/100')
    sys.exit(0)
score += 20
print('..中断和挂起正确')

print(f'评分结束,得分:{score}/100')
